package map.land;

import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;

/**
 * @ Author     ：侯耀祖
 * @ Description：
 */
public class bfsMain {
    public static int[][] dir ={{0,1},{1,0},{-1,0},{0,-1}};
    public static void bfs(boolean[][] visited, int[][] grid, int x, int y){
        Queue<int[]> queue = new LinkedList<>();
        queue.add(new int[]{x, y});
        visited[x][y] = true;
        while (!queue.isEmpty()){
            int X = queue.peek()[0];
            int Y = queue.poll()[1];
            for (int i = 0; i < 4; i++) {
                int nextX = X + dir[i][0];
                int nextY = Y + dir[i][1];
                if (nextX < 0 || nextY < 0 || nextX >= grid.length || nextY >= grid[0].length){
                    continue;
                }
                if (!visited[nextX][nextY] && grid[nextX][nextY] == 1){
                    queue.add(new int[]{nextX, nextY});
                    visited[nextX][nextY] = true;
                }
            }
        }
    }
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int rows = scanner.nextInt();
        int cols = scanner.nextInt();
        int[][] lands = new int[rows][cols];
        for (int i = 0; i < rows; i++) {
            for (int j = 0; j < cols; j++) {
                lands[i][j] = scanner.nextInt();
            }
        }
        boolean[][] visited = new boolean[rows][cols];
        int result = 0;
        for (int i = 0; i < rows; i++) {
            for (int j = 0; j < cols; j++) {
                if (!visited[i][j] && lands[i][j] == 1){
                    result++;
                    bfs(visited, lands, i, j);
                }
            }
        }
        System.out.println(result);
    }
}
